package com.base.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.common.AjaxResult;
import com.base.common.BaseController;
import com.base.common.TableDataInfo;
import com.base.domain.SysDept;
import com.base.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author gxd
 * @since 2025/6/6
 */
@Slf4j
@RestController
@RequestMapping("/dept")
@Api(tags = "部门信息")
public class SysDeptController extends BaseController {

    @Autowired
    private SysDeptService deptService;

    @ApiOperation("分页列表-部门列表")
    @PostMapping("/pageList")
    public TableDataInfo pageList(@ApiParam(value = "部门查询条件") SysDept dept) {
        startPage(dept.getPageNum(), dept.getPageSize());
        List<SysDept> deptList = deptService.pageList(dept);
        return getDataTable(deptList);
    }

    @ApiOperation("详情信息-根据Id获取部门详情信息")
    @ApiImplicitParam(name = "id", value = "部门Id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/getById/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(deptService.getById(id));
    }

    @ApiOperation("添加或修改-部门信息")
    @PostMapping("/addOrEdit")
    public AjaxResult addOrEdit(@ApiParam(value = "部门实体", required = true) @Valid @RequestBody SysDept dept) {
        return success(deptService.addOrEdit(dept));
    }

    @ApiOperation("删除-部门信息（支持批量）")
    @ApiImplicitParam(name = "ids", value = "部门Id（多个需逗号分隔）", required = true, allowMultiple = true, dataType = "String", paramType = "path")
    @DeleteMapping("/removeById")
    public AjaxResult removeById(@RequestParam("ids") String ids) {
        Assert.notEmpty("ids", "缺少必要参数：请提供要删除的记录ID");
        return toAjax(deptService.remove(Wrappers.lambdaQuery(SysDept.class).in(SysDept::getId, ids.split(StrPool.COMMA))));
    }
}
